home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / COMMS / C101.ZIP / UUPC11XS.ZIP / UUCICO / NBSTIME.C < prev    next >
C/C++ Source or Header  |  1992-11-12  |  11KB  |  321 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    n b s t i m e . c                                               */
  3. /*                                                                    */
  4. /*    Set local system clock from National Bureau of Standards        */
  5. /*    Standard Time service                                           */
  6. /*                                                                    */
  7. /*    Copyright (c) 1991, Andrew H. Derbyshire                        */
  8. /*    See README.PRN for distribution restrictions and additional     */
  9. /*    copyrights                                                      */
  10. /*--------------------------------------------------------------------*/
  11.  
  12. /*--------------------------------------------------------------------*/
  13. /*                        System include files                        */
  14. /*--------------------------------------------------------------------*/
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <time.h>
  19. #include <string.h>
  20.  
  21.  
  22. #ifdef FAMILYAPI
  23. #define INCL_BASE
  24. #ifdef WIN32
  25. #include <windows.h>
  26. #else
  27. #include <os2.h>
  28. #endif
  29. #else /* FAMILYAPI */
  30. #ifndef __TURBOC__
  31. #include <dos.h>
  32. #endif
  33. #endif
  34.  
  35.  
  36. /*--------------------------------------------------------------------*/
  37. /*                    UUPC/extended include files                     */
  38. /*--------------------------------------------------------------------*/
  39.  
  40. #include "lib.h"
  41. #include "arpadate.h"
  42. #include "dcp.h"
  43. #include "dcpsys.h"
  44. #include "hostable.h"
  45. #include "nbstime.h"
  46. #include "script.h"
  47. #include "security.h"
  48. #include "ulib.h"
  49.  
  50. #ifndef __TURBOC__
  51.       currentfile();
  52. #endif
  53. /*--------------------------------------------------------------------*/
  54. /*    n b s t i m e                                                   */
  55. /*                                                                    */
  56. /*    Set system clock from using time from NBS of the format:        */
  57. /*                                                                    */
  58. /*                  MJD  YR MO DA  H  M  S ST S UT1 msADV         OTM */
  59. /*  nbs format-->  47511 88-12-16 06:03:44 00 0 -.1 045.0 UTC(NIST) * */
  60. /*  @ 1200 baud    47511 88-12-16 06:03:45 00 0 -.1 045.0 UTC(NIST) * */
  61. /*--------------------------------------------------------------------*/
  62.  
  63. boolean nbstime( void )
  64. {
  65.    char buf[BUFSIZ];
  66.    time_t today;
  67.    struct tm  tx;
  68.    int cycles = 15;
  69.    int dst= 0;
  70.    time_t delta;
  71.    char sync = '?';
  72. #ifdef FAMILYAPI
  73. #ifdef WIN32
  74.    SYSTEMTIME DateTime;
  75.    TOKEN_PRIVILEGES tkp;
  76.    HANDLE hToken;
  77. #else
  78.    DATETIME DateTime;
  79. #endif
  80.    struct tm *tp;
  81.    USHORT rc;
  82. #else
  83. #ifndef __TURBOC__
  84.    unsigned short rc;
  85.    struct tm *tp;
  86.    struct dosdate_t ddate;
  87.    struct dostime_t dtime;
  88. #endif
  89. #endif
  90.  
  91.    memset( &tx , '\0', sizeof tx);        /* Clear pointers          */
  92.    if (!expectstr("MJD", 5, NULL )) /* Margaret Jane Derbyshire? :-) */
  93.    {
  94.       printmsg(0,"nbstime: Did not find MJD literal in data from remote");
  95.       return FALSE;
  96.    }
  97.  
  98.    rmsg(buf, 2, 2, sizeof buf);
  99.                   /* Read one line to get us setup for input   */
  100.  
  101. /*--------------------------------------------------------------------*/
  102. /*                  Begin main loop to get the time                   */
  103. /*--------------------------------------------------------------------*/
  104.  
  105.    while ((rmsg(buf, 2, 2, sizeof buf) != TIMEOUT) && cycles--)
  106.    {
  107.       sync = buf[ strlen( buf ) - 1 ];
  108.  
  109.       if (sync == '#')
  110.          break;
  111.       else if (sync != '*')
  112.          *buf = '\0';
  113.  
  114.    } /* while */
  115.  
  116.    if ( (cycles && (sync == '*')) || (*buf == '\0'))
  117.    {
  118.       printmsg(0,"nbstime: Did not get good buffer: \"%s\"", buf );
  119.                   return FALSE;
  120.    }
  121.  
  122. /*--------------------------------------------------------------------*/
  123. /*                   Determine the time we received                   */
  124. /*--------------------------------------------------------------------*/
  125.  
  126.    sscanf(buf,"%*s %d-%d-%d %d:%d:%d %d ",
  127.          &tx.tm_year, &tx.tm_mon, &tx.tm_mday ,
  128.          &tx.tm_hour, &tx.tm_min, &tx.tm_sec, &dst);
  129.    tx.tm_mon--;               /* Tm record counts months from zero   */
  130.  
  131.    today = mktime(&tx);       /* Current UTC (GMT) time in seconds   */
  132.  
  133.    if ( debuglevel > 2 )
  134.    {
  135.       printmsg(3,"%2d/%2d/%2d %2d:%2d:%2d %2d %c translates to %ld or %s",
  136.          tx.tm_year, tx.tm_mon + 1 , tx.tm_mday ,
  137.          tx.tm_hour, tx.tm_min, tx.tm_sec, dst, sync ,
  138.          today, ctime( &today ));
  139.    }
  140.  
  141. /*--------------------------------------------------------------------*/
  142. /*    Perform a sanity check; the time must be 20 years past 1970     */
  143. /*--------------------------------------------------------------------*/
  144.  
  145.    if ( today < 630720000L )
  146.    {
  147.       printmsg(0,"nbstime: Time warp error (%s), clock not set",
  148.             ctime( &today ));
  149.       return FALSE;
  150.    }
  151.  
  152. /*--------------------------------------------------------------------*/
  153. /*     Borland C++ doesn't set the time properly; do a conversion     */
  154. /*--------------------------------------------------------------------*/
  155.  
  156.    today -= timezone;
  157.  
  158. /*--------------------------------------------------------------------*/
  159. /*                        Set the system clock                        */
  160. /*--------------------------------------------------------------------*/
  161.  
  162. #ifdef FAMILYAPI
  163.    tp = localtime(&today);    /* Get local time as a record          */
  164. #ifdef WIN32
  165.    GetSystemTime( &DateTime );
  166. #else
  167.    rc = DosGetDateTime( &DateTime );
  168.    if ( rc != 0 )
  169.    {
  170.       printmsg(0,"Return code from DosGetDateTime %d", rc);
  171.       panic();
  172.    }
  173.  
  174. #endif
  175. #ifdef WIN32
  176.    printmsg(3,"Date time: %2d/%2d/%2d %2d:%2d:%2d, weekday %d",
  177.       (int) DateTime.wYear, (int) DateTime.wMonth, (int) DateTime.wDay ,
  178.       (int) DateTime.wHour, (int) DateTime.wMinute,(int) DateTime.wSecond ,
  179.       (int) DateTime.wDayOfWeek );
  180.  
  181.    DateTime.wYear    = (USHORT) tp->tm_year + 1900;
  182.    DateTime.wMonth   = (UCHAR) (tp->tm_mon + 1);
  183.    DateTime.wDay     = (UCHAR) tp->tm_mday;
  184.    DateTime.wHour    = (UCHAR) tp->tm_hour;
  185.    DateTime.wMinute  = (UCHAR) tp->tm_min;
  186.    DateTime.wSecond  = (UCHAR) tp->tm_sec;
  187.    printmsg(3,"Date time: %2d/%2d/%2d %2d:%2d:%2d, weekday %d",
  188.       (int) DateTime.wYear, (int) DateTime.wMonth, (int) DateTime.wDay ,
  189.       (int) DateTime.wHour, (int) DateTime.wMinute,(int) DateTime.wSecond ,
  190.       (int) DateTime.wDayOfWeek );
  191. #else
  192.    printmsg(3,"Date time: %2d/%2d/%2d %2d:%2d:%2d tz %d, weekday %d",
  193.       (int) DateTime.year, (int) DateTime.month, (int) DateTime.day ,
  194.       (int) DateTime.hours, (int) DateTime.minutes,(int) DateTime.seconds ,
  195.       (int) DateTime.timezone, (int) DateTime.weekday );
  196.  
  197.    DateTime.year    = (USHORT) tp->tm_year + 1900;
  198.    DateTime.month   = (UCHAR) (tp->tm_mon + 1);
  199.    DateTime.day     = (UCHAR) tp->tm_mday;
  200.    DateTime.hours   = (UCHAR) tp->tm_hour;
  201.    DateTime.minutes = (UCHAR) tp->tm_min;
  202.    DateTime.seconds = (UCHAR) tp->tm_sec;
  203.    printmsg(3,"Date time: %2d/%2d/%2d %2d:%2d:%2d tz %d, weekday %d",
  204.       (int) DateTime.year, (int) DateTime.month, (int) DateTime.day ,
  205.       (int) DateTime.hours, (int) DateTime.minutes,(int) DateTime.seconds ,
  206.       (int) DateTime.timezone, (int) DateTime.weekday );
  207. #endif
  208.  
  209. #ifdef WIN32  /* See July edition of Programmer's Overview, p. 268-69 */
  210.  
  211.    if (!OpenProcessToken(GetCurrentProcess(),
  212.       TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  213.       &hToken))
  214.    {
  215.       printmsg(0, "nbstime: OpenProcessToken failed: error = %u\n",
  216.          GetLastError());
  217.       return FALSE;
  218.    }
  219.  
  220.    LookupPrivilegeValue(NULL, "SeSystemtimePrivilege",
  221.       &tkp.Privileges[0].Luid);
  222.    tkp.PrivilegeCount = 1;
  223.    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  224.  
  225.    if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  226.       (PTOKEN_PRIVILEGES)NULL, 0))
  227.    {
  228.       printmsg(0, "nbstime: first AdjustTokenPrivilege failed: returned %u\n",
  229.          GetLastError());
  230.       return FALSE;
  231.    }
  232.  
  233.    rc = SetSystemTime( &DateTime );
  234.    if ( rc != 0 )
  235.    {
  236.       printmsg(0, "nbstime: Unable to set time\n");
  237.    }
  238.  
  239.    tkp.Privileges[0].Attributes = 0;
  240.    
  241.    if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  242.       (PTOKEN_PRIVILEGES)NULL, 0))
  243.    {
  244.       printmsg(0, "nbstime: AdjustTokenPrivilege disable failed: returned %u\n",
  245.          GetLastError());
  246.       return FALSE;
  247.    }
  248.  
  249. #else /* WIN32 */
  250.    rc = DosSetDateTime( &DateTime );
  251.    if ( rc != 0 )
  252.    {
  253.       printmsg(0,"Return code from DosGetDateTime %d", rc);
  254.       panic();
  255.    }
  256. #endif /* WIN32 */
  257. #else /* FAMILYAPI */
  258. #ifdef __TURBOC__
  259. /*--------------------------------------------------------------------*/
  260. /*    If this timezone uses daylight savings and we are in the        */
  261. /*    period to spring forward, do so.                                */
  262. /*--------------------------------------------------------------------*/
  263.  
  264.    if (daylight && ( dst > 1 ) && ( dst < 52 ))
  265.       today += 3600;          /* This is valid for the USA only      */
  266.    stime( &today );
  267. #else /* __TURBOC__ */
  268.    tp = localtime(&today);    /* Get local time as a record          */
  269.  
  270.    ddate.day     = (unsigned char) tp->tm_mday;
  271.    ddate.month   = (unsigned char) (tp->tm_mon + 1);
  272.    ddate.year    = (unsigned int)  (tp->tm_year + 1900);
  273.    ddate.dayofweek = (unsigned char) tp->tm_wday;       /* 0-6, 0=Sunday */
  274.  
  275.    dtime.hour    = (unsigned char) tp->tm_hour;
  276.    dtime.minute  = (unsigned char) tp->tm_min;
  277.    dtime.second  = (unsigned char) tp->tm_sec;
  278.    dtime.hsecond = (unsigned char) 0;
  279.  
  280.    printmsg(3,"Date time: %2d/%2d/%2d %2d:%2d:%2d tz %d, weekday %d",
  281.       (int) ddate.year, (int) ddate.month, (int) ddate.day ,
  282.       (int) dtime.hour, (int) dtime.minute,(int) dtime.second ,
  283.       (int) timezone, (int) ddate.dayofweek );
  284.  
  285.    if ( (rc = _dos_settime( &dtime )) != 0 )
  286.    {
  287.       printmsg(0,"Return code from _dos_settime %d", rc);
  288.       panic();
  289.    }
  290.  
  291.    if ( (rc = _dos_setdate( &ddate )) != 0 )
  292.    {
  293.       printmsg(0,"Return code from _dos_setdate %d", rc);
  294.       panic();
  295.    }
  296.  
  297. #endif /* __TURBOC__ */
  298. #endif /* FAMILYAPI */
  299.  
  300. /*--------------------------------------------------------------------*/
  301. /*             Print debugging information, if requested              */
  302. /*--------------------------------------------------------------------*/
  303.  
  304.    delta = today - time( NULL );
  305.    printmsg(2,"nbstime: \"%s\"", buf);
  306.    printmsg(2,"nbstime: Time delta is %ld seconds, zone offset %ld, \
  307. daylight savings %d",
  308.                   delta, timezone, dst );
  309.  
  310.    if ( sync == '*' )
  311.       printmsg(2,"Warning: Was unable to synchonize with NBS master");
  312.  
  313. /*--------------------------------------------------------------------*/
  314. /*                Announce new time, return to caller                 */
  315. /*--------------------------------------------------------------------*/
  316.  
  317.    printmsg(0,"nbstime: New system time is %s", arpadate());
  318.    return TRUE;
  319.  
  320. } /* nbstime */
  321.